package com.huanzhidadi.singleweb.fastjson;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import com.huanzhidadi.singleweb.fastjson.filter.IgnorePropertyFilter;
import com.huanzhidadi.singleweb.fastjson.filter.NullFilter;
import com.huanzhidadi.singleweb.fastjson.filter.TrimFilter;
import com.huanzhidadi.singleweb.fastjson.model.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "fastjson数据处理tags")
@RequestMapping(value = "fastjson")
public class FastjsonController {

    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected HttpServletRequest request;

    /**
     * {
     *   "aaa": "  123  ",
     *   "bbb": "null",
     *   "ccc": null
     * }
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "过滤json串内容value", notes = "过滤json串内容notes")
    @RequestMapping(value = "/getJsonString", method = RequestMethod.POST)
    public String getJsonString(@RequestBody JSONObject jsonObject) {
        List<Filter> filterList = new ArrayList<>();
        filterList.add(new TrimFilter());
        filterList.add(new NullFilter());
        filterList.add(new IgnorePropertyFilter());
        Filter[] filters = filterList.toArray(new Filter[0]);
        log.info(jsonObject.getString("aaa"));
        String jsonStr = JSON.toJSONString(jsonObject, filters, JSONWriter.Feature.WriteMapNullValue);
        return jsonStr;
    }

    /**
     * 要注意过滤器顺序会影响eee的内容
     * [
     * {
     *   "aaa": "  123  ",
     *   "bbb": "null",
     *   "ccc": null
     * },
     * {
     *   "aaa": "  aa",
     *   "bbb": "null",
     *   "ccc": null,
     *   "ddd": "dd  ",
     *   "eee": "  null  "
     * }
     * ]
     *
     * @param jsonArray
     * @return
     */
    @ApiOperation(value = "过滤json串数组内容value", notes = "过滤json串数组内容notes")
    @RequestMapping(value = "/getJsonArrayString", method = RequestMethod.POST)
    public String getJsonArrayString(@RequestBody JSONArray jsonArray) {
        List<Filter> filterList = new ArrayList<>();
        filterList.add(new TrimFilter());
        filterList.add(new NullFilter());
        filterList.add(new IgnorePropertyFilter());
        Filter[] filters = filterList.toArray(new Filter[0]);
        // SerializerFeature.WriteMapNullValue 如果值为空，也正常显示
        String jsonStr = JSON.toJSONString(jsonArray, filters, JSONWriter.Feature.WriteMapNullValue);
        return jsonStr;
    }

    /**
     * {
     *   "name": "  wang  ",
     *   "age": 22
     * }
     *
     * @param student
     * @return
     */
    @ApiOperation(value = "对象内容转json串value", notes = "对象内容转json串notes")
    @RequestMapping(value = "/getObjectString", method = RequestMethod.POST)
    public String getObjectString(@RequestBody Student student) {
        List<Filter> filterList = new ArrayList<>();
        filterList.add(new TrimFilter());
        filterList.add(new NullFilter());
        filterList.add(new IgnorePropertyFilter());
        Filter[] filters = filterList.toArray(new Filter[0]);
        log.info(student.getName());
        // WriteNullStringAsEmpty只对对象内容有效，非对象的话需要自定义一个Filter对null进行处理
        String jsonStr = JSON.toJSONString(student, filters, JSONWriter.Feature.WriteNullStringAsEmpty, JSONWriter.Feature.WriteMapNullValue);
        return jsonStr;
    }

}
